﻿
// ScreenShotDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ScreenShot.h"
#include "ScreenShotDlg.h"
#include "afxdialogex.h"
#include "CShowDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define WM_NOTIFYICON WM_USER + 1//自定义消息


// CScreenShotDlg 对话框



CScreenShotDlg::CScreenShotDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SCREENSHOT_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CScreenShotDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CScreenShotDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CScreenShotDlg::OnBnClickedButton1)
	ON_WM_DESTROY()
	ON_MESSAGE(WM_NOTIFYICON, &CScreenShotDlg::OnNotifyIcon)
	ON_WM_HOTKEY()
END_MESSAGE_MAP()


// CScreenShotDlg 消息处理程序

BOOL CScreenShotDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	m_notifyIconData.cbSize = sizeof(NOTIFYICONDATA);//结构体大小
	m_notifyIconData.hWnd = m_hWnd;//窗口句柄
	m_notifyIconData.uID = IDR_MAINFRAME;//图标ID
	m_notifyIconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;//图标属性
	m_notifyIconData.uCallbackMessage = WM_NOTIFYICON;//自定义消息
	m_notifyIconData.hIcon = m_hIcon;//图标句柄
	wcscpy_s(m_notifyIconData.szTip, L"截图工具");//鼠标悬停提示

	//设置托盘
	Shell_NotifyIcon(NIM_ADD, &m_notifyIconData);//添加托盘图标
	//全局热键
	hotKeyID = GlobalAddAtom(L"ScreenShot");//注册热键ID
	if (!RegisterHotKey(m_hWnd, hotKeyID, MOD_CONTROL | MOD_ALT, 'S'))
		MessageBox(L"注册热键Ctl+Alt+S失败");
	//把窗口移到左上角
	MoveWindow(-100, -100, -30, -30);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CScreenShotDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CScreenShotDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CScreenShotDlg::OnBnClickedButton1()
{
	CShowDialog dlg;
	dlg.DoModal();
}


void CScreenShotDlg::OnDestroy()
{
	CDialogEx::OnDestroy();

	//销毁托盘图标
	Shell_NotifyIcon(NIM_DELETE, &m_notifyIconData);
}

LRESULT CScreenShotDlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
	switch (lParam)
	{
	case WM_LBUTTONDBLCLK://双击托盘图标
		ShowWindow(SW_SHOW);
		break;
	case WM_RBUTTONDOWN://右键点击托盘图标
	{
		//获取当前鼠标坐标
		CPoint point;
		GetCursorPos(&point);
		//创建菜单
		CMenu menu;
		menu.LoadMenu(IDR_MENU1);
		CMenu* pMenu = menu.GetSubMenu(0);//获取第一个子菜单
		pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_BOTTOMALIGN, point.x, point.y, this);//在鼠标位置弹出菜单
		break;
	}
	}
	return 0;
}


void CScreenShotDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
	if (nHotKeyId == hotKeyID)//判断是否是注册的热键
	{
		OnBnClickedButton1();//执行截图

	}

	CDialogEx::OnHotKey(nHotKeyId, nKey1, nKey2);
}
